<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ajax文件上传</title>
</head>
<body>
<p>在 demo.html 的基础上增加一个进度显示，注意浏览器标题栏。</p>
<form action="upload.php">
<input type="file" id="file">
</form>
<script src="jquery-1.11.0.js"></script>
<script>
$('#file').change(function(){
	var files = this.files
	var file = files[0]
	
	var data = new FormData()
	data.append("name", encodeURIComponent(file.name))
	data.append("file", file)
			
	var xhr = new XMLHttpRequest()
	xhr.open("post", 'upload.php')
	
	// 在 demo.html 的基础上增加一个进度显示
	var total = 0
	xhr.upload.onprogress = function(e) {
		total += e.loaded
		document.title = '上传 ' + total + ' 字节'
	}
	
	xhr.send(data)
})
</script>
</body>
</html>